package com.ruoyi.web.controller.mes.report;
import com.alibaba.druid.sql.visitor.functions.Left;
import com.alibaba.druid.sql.visitor.functions.Substring;
import com.alibaba.excel.util.MapUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.filedown;
import com.ruoyi.web.domain.mes.report.PersionLing;
import com.ruoyi.web.domain.mes.report.PersionSalary;
import com.ruoyi.web.domain.mes.report.PersionSalaryDetails;
import com.ruoyi.web.domain.mes.report.Personnelpayday;
import com.ruoyi.web.service.Impl.PersionSalaryServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/mes/report/persion")
public class PersionReportController extends BaseController {
    @Autowired
    private PersionSalaryServiceImpl persionSalaryService;

    @GetMapping("/getpersionsalarylist")
    public TableDataInfo getpersionsalarylist(PersionSalary salary) {
        startPage();  // 此方法配合前端完成自动分页
        List<PersionSalary> list = persionSalaryService.GetPersionSalary(salary);
        return getDataTable(list);
    }


    @GetMapping("/getpersionsalarydetailslist")
    public TableDataInfo getpersionsalarydetailslist(PersionSalary salary) {
        System.out.println("------>>>" + salary.getName());
        System.out.println("------>>>" + salary.getMonth());
        startPage();  // 此方法配合前端完成自动分页
        List<PersionSalaryDetails> list = persionSalaryService.GetPersionSalaryDetails(salary);
        return getDataTable(list);
    }



    @GetMapping("/Printdaily")
    public TableDataInfo  getPrintdaily(PersionSalary salary){
        startPage();  // 此方法配合前端完成自动分页
        salary.setMonth(salary.getYear().substring(5,7));
        salary.setYear(salary.getYear().substring(0,4));
        List<Personnelpayday> list = persionSalaryService.GetPersionSalaryDetailsexport(salary);
        for (int a=0;a<list.size();a++){
            list.get(a).setTotal(format6(list.get(a).getTotals()));
        }
        return getDataTable(list);
    }


    @PostMapping(value = "daochu")
    public void daochu(HttpServletResponse response,@RequestBody PersionSalary salary) throws IOException {
        List<Personnelpayday> list=null;
        if (salary!=null){
            list= persionSalaryService.GetPersionSalaryDetailsexport(salary);
        }
/*
* 获取模版位置
* */
  InputStream inputStream=PersionReportController.class.getClassLoader().getResourceAsStream("temlates/excelemport.xlsx");
        filedown  filedownexcel=new filedown();
        String name="工资明细";
        String date=salary.getYear()+"年"+salary.getMonth()+"月";
        Map<String,Object> map= MapUtils.newHashMap();
        map.put("date",date);
        /*
        * 引入方法传入集合、response,模版的输入流
        * */
        filedownexcel.filedownexcels(list,response,name,inputStream,map);
    }


    @PostMapping(value = "daochuhj")
    public void daochuhj(HttpServletResponse response,@RequestBody PersionSalary salary) throws IOException {
        List<PersionSalary> list=null;
        if (salary!=null){
            list= persionSalaryService.GetPersionSalary(salary);

//                    persionSalaryService.GetPersionSalaryDetailsexport(salary);
        }
        for ( PersionSalary persionSalary :list){
            persionSalary.setHj(persionSalary.getOnemonth()+persionSalary.getTenmonth()+persionSalary.getThreemonth()+ persionSalary.getFourmonth()+
                    persionSalary.getFivemonth()+persionSalary.getSixmonth()+persionSalary.getSevenmonth()+persionSalary.getEightmonth()+
                    persionSalary.getNinemonth()+persionSalary.getTenmonth()+persionSalary.getElevenmonth()+persionSalary.getTwelvemonth());
        }
        /*
         * 获取模版位置
         * */
        InputStream inputStream=PersionReportController.class.getClassLoader().getResourceAsStream("temlates/excelsalehj.xlsx");
        filedown  filedownexcel=new filedown();
        String name="工资合计";
        String date=salary.getYear()+"年";
        Map<String,Object> map= MapUtils.newHashMap();
        map.put("date",date);
        /*
         * 引入方法传入集合、response,模版的输入流
         * */
        filedownexcel.filedownexcels(list,response,name,inputStream,map);
    }



    @PostMapping(value = "exportprint")
    public void exportprint(HttpServletResponse response,@RequestBody PersionSalary salary) throws IOException {



        salary.setMonth(salary.getYear().substring(5,7));
        salary.setYear(salary.getYear().substring(0,4));
        List<Personnelpayday> list=null;
        if (salary!=null){
            list= persionSalaryService.GetPersionSalaryDetailsexport(salary);
            for (int a=0;a<list.size();a++){
                list.get(a).setXh(a+1);
                list.get(a).setTotal(format6(list.get(a).getTotals()));
                if (list.get(a).getWorkshopname()==1){
                    list.get(a).setWorkshopnamechs("一车间");
                }else if(list.get(a).getWorkshopname()==2){
                    list.get(a).setWorkshopnamechs("二车间");
                }else if(list.get(a).getWorkshopname()==3){
                    list.get(a).setWorkshopnamechs("三车间");
                }else if(list.get(a).getWorkshopname()==4){
                    list.get(a).setWorkshopnamechs("四车间");
                }
            }
        }
        /*
         * 获取模版位置
         * */

        InputStream inputStream=PersionReportController.class.getClassLoader().getResourceAsStream("temlates/printexport.xlsx");
        filedown  filedownexcel=new filedown();
        String name="工资明细";

        String date= salary.getYear();
        System.out.println(date+"--------->");


        Map<String,Object> map= MapUtils.newHashMap();
        map.put("date",date);
        map.put("producetechnicsgy",salary.getProducetechnicsgy());
        /*
         * 引入方法传入集合、response,模版的输入流
         * */
        filedownexcel.filedownexcels(list,response,name,inputStream,map);
    }


    @GetMapping (value ="persionling")
    public TableDataInfo   getpersionling(PersionLing persionLing){
//        startPage();  // 此方法配合前端完成自动分页
        persionLing.setYear("2024");
        List<PersionLing> list1=persionSalaryService.GetPersionLingList(persionLing);
        return getDataTable(list1);
    }



    /*四舍五入*/
    public static String format6(double value) {
String newdouble=String.format("%.2f", value);
        return newdouble;
    }




}

